#include <SDL2/SDL.h>
#include "canvas.hh"

int main()
{
	SDL_Init(SDL_INIT_EVERYTHING);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE  , 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE , 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	auto win = SDL_CreateWindow("flat canvas",
					SDL_WINDOWPOS_CENTERED,
					SDL_WINDOWPOS_CENTERED,
					16*50, 9*50,
					SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
	SDL_GL_CreateContext(win);
	SDL_GL_SetSwapInterval(1);

	canvas::init();

	int w, h;
	SDL_GL_GetDrawableSize(win, &w, &h);
	canvas::resize(w, h);

	while (true) {
		SDL_Event e;
		while (SDL_PollEvent(&e))
			switch (e.type) {
				case SDL_QUIT: goto _quit;
				case SDL_KEYDOWN:
					switch (e.key.keysym.sym) {
						case '\e':
						case 'q':
							goto _quit;
					}
					break;
				case SDL_WINDOWEVENT:
					if (e.window.event == SDL_WINDOWEVENT_RESIZED) {
						SDL_GL_GetDrawableSize(win, &w, &h);
						canvas::resize(w, h);
					}
					break;
			}

		canvas::render(SDL_GetTicks() / 1000.0f);
		SDL_GL_SwapWindow(win);
	}

_quit:
	SDL_Quit();
}

